package com.api.oa.services.impl;

import cn.hutool.core.date.DateUtil;
import com.api.oa.common.UserType;
import com.api.oa.config.UserContext;
import com.api.oa.mapper.*;
import com.api.oa.model.entity.*;
import com.api.oa.model.vo.OaOrgTreeVO;
import com.api.oa.model.vo.YQFaceAttendVO;
import com.api.oa.model.vo.YQRequestVO;
import com.api.oa.services.YQFaceAttendService;
import com.api.oa.utils.CoordinateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;


@Service
public class YQFaceAttendServiceImpl implements YQFaceAttendService {

    @Autowired
    private YQFaceAttendMapper mapper;
    @Autowired
    private YQPatientMapper yqPatientMapper;
    @Autowired
    YQInsEntityMapper yqInsEntityMapper;
    @Autowired
    YQMedCareMapper yqMedCareMapper;
    @Autowired
    YQRequestMapper yqRequestMapper;
    @Autowired
    YQUserMapper yqUserMapper;


    @Value("${minDis}")
    private double minDis;


    @Override
    public Integer add(YQFaceAttend entity) throws Exception {
        YQUser user = yqUserMapper.findOne(entity.getUserId());
        Integer userType = user.getUserType();

        if (entity.getType() == 0) { //机构签到 医护，患者（院护）
            String insCode = "";
            if (userType == UserType.NUR || userType == UserType.MED || userType == UserType.CARE) { //医护
                YQMedCare care = yqMedCareMapper.findOne(entity.getUserId());
                if (care == null) throw new Exception("医护不存在");
                insCode = care.getMedIns();
            } else if (userType == UserType.PATIENT) {  //患者
                YQPatient one = yqPatientMapper.findOne(entity.getUserId());
                if (one == null) throw new Exception("患者不存在");
                insCode = one.getPatientIns();
            }
            YQInsEntity one1 = yqInsEntityMapper.findOne(insCode);
            if (one1 == null) throw new Exception("您签约的结构不存在");
            double v = CoordinateUtil.latLngDist(entity.getLng(), entity.getLat(), one1.getLng(), one1.getLat());
            entity.setStatus(v > minDis ? 1 : 0); //异常
            entity.setInsCode(insCode);
        } else if (entity.getType() == 1) {  //去服务
            //查找患者家里
            YQPatient one = yqPatientMapper.findOne(entity.getPatientUserId());
            if (one == null) throw new Exception("患者不存在");
            double v = CoordinateUtil.latLngDist(entity.getLng(), entity.getLat(), one.getLng(), one.getLat());
            entity.setStatus(v > minDis ? 1 : 0); //异常
            entity.setInsCode(one.getPatientIns());
        } else if (entity.getType() == 2) {  // 评估
            YQRequestVO one = yqRequestMapper.findOne(entity.getOrderCode());
            if (one == null) throw new Exception("申请单不存在");
            double v = CoordinateUtil.latLngDist(entity.getLng(), entity.getLat(), one.getLng(), one.getLat());
            entity.setStatus(v > minDis ? 1 : 0); //异常
            entity.setInsCode(one.getRequestIns());
        }

        entity.setCreatedAt(DateUtil.date());
        return mapper.insert(entity);
    }

    @Override
    public void remove(Long id) {
        mapper.delete(id);
    }

    @Override
    public Integer update(YQFaceAttend entity) {
        return mapper.update(entity);
    }

    @Override
    public PageInfo<YQFaceAttendVO> list(String insCode, String status, String type, String role, Integer pageIndex, Integer pageSize) {
        if (UserContext.getRole() == UserType.INS) {
            insCode = UserContext.getInsCode();
        }
        PageHelper.startPage(pageIndex, pageSize);
        PageHelper.orderBy("attendId desc");
        return new PageInfo<>(mapper.list(insCode, status,  role,type));
    }

    @Override
    public YQFaceAttend one(Long id) {
        return mapper.findOne(id);
    }

    @Override
    public List<String> findWeekAttend(Long userId) {
        return mapper.findWeekAttend(userId);
    }


}
